// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   change_ok.java

package oa.publicize;

import com.jspsmart.upload.*;
import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;

public class change_ok extends HttpServlet
{

	public change_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			ServletContext context = session.getServletContext();
			String path = context.getRealPath("/");
			counter count = new counter(dbApplication);
			SmartUpload mySmartUpload = new SmartUpload();
			mySmartUpload.setCharset("UTF-8");
			nseer_db_backup1 oa_db = new nseer_db_backup1(dbApplication);
			if (oa_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String publicize_ID = request.getParameter("publicize_ID");
				mySmartUpload.initialize(pageContext);
				String file_type = getFileLength.getFileType((String)session.getAttribute("unit_db_name"));
				long d = getFileLength.getFileLength((String)session.getAttribute("unit_db_name"));
				mySmartUpload.setMaxFileSize(d);
				mySmartUpload.setAllowedFilesList(file_type);
				try
				{
					mySmartUpload.upload();
					String file_name[] = new String[mySmartUpload.getFiles().getCount()];
					String not_change[] = new String[mySmartUpload.getFiles().getCount()];
					String fileKind_chain = mySmartUpload.getRequest().getParameter("fileKind_chain");
					String chain_id = Divide1.getId(fileKind_chain);
					String chain_name = Divide1.getName(fileKind_chain);
					String sql1 = (new StringBuilder("select * from oa_publicize where publicize_ID='")).append(publicize_ID).append("' and check_tag='1'").toString();
					ResultSet rs = oa_db.executeQuery(sql1);
					if (!rs.next())
					{
						response.sendRedirect((new StringBuilder("oa/publicize/change_ok_a.jsp?publicize_ID=")).append(publicize_ID).toString());
					} else
					{
						String attachment[] = mySmartUpload.getRequest().getParameterValues("attachment");
						String delete_file_name[] = new String[0];
						if (attachment != null)
						{
							delete_file_name = new String[attachment.length];
							for (int i = 0; i < attachment.length; i++)
								delete_file_name[i] = rs.getString(attachment[i]);

						}
						for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
						{
							SmartFile file = mySmartUpload.getFiles().getFile(i);
							if (file.isMissing())
							{
								file_name[i] = "";
								int q = i + 1;
								String field_name = (new StringBuilder("attachment")).append(q).toString();
								if (!rs.getString(field_name).equals(""))
									not_change[i] = "yes";
							} else
							{
								int filenum = count.read((String)dbSession.getAttribute("unit_db_name"), "documentAttachmentcount");
								count.write((String)dbSession.getAttribute("unit_db_name"), "documentAttachmentcount", filenum);
								file_name[i] = (new StringBuilder(String.valueOf(filenum))).append(file.getFileName()).toString();
								file.saveAs((new StringBuilder(String.valueOf(path))).append("oa\\file_attachments\\").append(filenum).append(file.getFileName()).toString());
							}
						}

						String changer = mySmartUpload.getRequest().getParameter("changer");
						String changer_ID = mySmartUpload.getRequest().getParameter("changer_ID");
						String change_time = mySmartUpload.getRequest().getParameter("change_time");
						String content = mySmartUpload.getRequest().getParameter("content");
						String bodyab = new String(mySmartUpload.getRequest().getParameter("remark").getBytes("UTF-8"), "UTF-8");
						String remark = exchange.toHtml(bodyab);
						String lately_change_time = mySmartUpload.getRequest().getParameter("lately_change_time");
						String file_change_amount = mySmartUpload.getRequest().getParameter("change_amount");
						int change_amount = Integer.parseInt(file_change_amount) + 1;
						String sql3 = (new StringBuilder("select * from oa_publicize where publicize_ID='")).append(publicize_ID).append("'").toString();
						ResultSet rs3 = oa_db.executeQuery(sql3);
						if (rs3.next())
						{
							String sql4 = (new StringBuilder("insert into oa_publicize_dig(publicize_ID,subject,type,chain_id,chain_name,CONTENT,attachment1,attachment2,remark,register,register_ID,CHECKER,CHECKER_ID,CHANGER,CHANGER_ID,register_TIME,CHECK_TIME,CHANGE_TIME,LATELY_CHANGE_TIME,change_amount,CHECK_TAG) values('")).append(rs3.getString("publicize_ID")).append("','").append(rs3.getString("subject")).append("','").append(rs3.getString("type")).append("','").append(rs3.getString("chain_id")).append("','").append(rs3.getString("chain_name")).append("','").append(rs3.getString("CONTENT")).append("','").append(rs3.getString("attachment1")).append("','").append(rs3.getString("attachment2")).append("','").append(rs3.getString("remark")).append("','").append(rs3.getString("register")).append("','").append(rs3.getString("register_ID")).append("','").append(rs3.getString("CHECKER")).append("','").append(rs3.getString("CHECKER_ID")).append("','").append(rs3.getString("CHANGER")).append("','").append(rs3.getString("CHANGER_ID")).append("','").append(rs3.getString("register_TIME")).append("','").append(rs3.getString("CHECK_TIME")).append("','").append(rs3.getString("CHANGE_TIME")).append("','").append(rs3.getString("LATELY_CHANGE_TIME")).append("','").append(rs3.getString("change_amount")).append("','").append(rs3.getString("CHECK_TAG")).append("')").toString();
							oa_db.executeUpdate(sql4);
						}
						List rsList = GetWorkflow.getList(oa_db, "oa_config_workflow", "02");
						String elem[] = new String[3];
						if (rsList.size() == 0)
						{
							String sqla = (new StringBuilder("update oa_publicize set changer='")).append(changer).append("',changer_ID='").append(changer_ID).append("',change_time='").append(change_time).append("',content='").append(content).append("',remark='").append(remark).append("',lately_change_time='").append(lately_change_time).append("',change_amount='").append(change_amount).append("',chain_id='").append(chain_id).append("',chain_name='").append(chain_name).append("',check_tag='1'").toString();
							String sqlb = (new StringBuilder(" where publicize_ID='")).append(publicize_ID).append("'").toString();
							if (attachment != null)
							{
								for (int i = 0; i < attachment.length; i++)
								{
									sqla = (new StringBuilder(String.valueOf(sqla))).append(",").append(attachment[i]).append("=''").toString();
									File file = new File((new StringBuilder(String.valueOf(path))).append("oa\\file_attachments\\").append(delete_file_name[i]).toString());
									file.delete();
								}

							}
							for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
								if (not_change[i] == null || !not_change[i].equals("yes"))
								{
									int p = i + 1;
									sqla = (new StringBuilder(String.valueOf(sqla))).append(",attachment").append(p).append("='").append(file_name[i]).append("'").toString();
								}

							String sql = (new StringBuilder(String.valueOf(sqla))).append(sqlb).toString();
							oa_db.executeUpdate(sql);
						} else
						{
							String sql2 = (new StringBuilder("delete from oa_workflow where object_ID='")).append(publicize_ID).append("' and type_id='02'").toString();
							oa_db.executeUpdate(sql2);
							String sqla = (new StringBuilder("update oa_publicize set changer='")).append(changer).append("',changer_ID='").append(changer_ID).append("',change_time='").append(change_time).append("',content='").append(content).append("',remark='").append(remark).append("',lately_change_time='").append(lately_change_time).append("',change_amount='").append(change_amount).append("',chain_id='").append(chain_id).append("',chain_name='").append(chain_name).append("',check_tag='0'").toString();
							String sqlb = (new StringBuilder(" where publicize_ID='")).append(publicize_ID).append("'").toString();
							if (attachment != null)
							{
								for (int i = 0; i < attachment.length; i++)
								{
									sqla = (new StringBuilder(String.valueOf(sqla))).append(",").append(attachment[i]).append("=''").toString();
									File file = new File((new StringBuilder(String.valueOf(path))).append("oa\\file_attachments\\").append(delete_file_name[i]).toString());
									file.delete();
								}

							}
							for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
								if (not_change[i] == null || !not_change[i].equals("yes"))
								{
									int p = i + 1;
									sqla = (new StringBuilder(String.valueOf(sqla))).append(",attachment").append(p).append("='").append(file_name[i]).append("'").toString();
								}

							String sql = (new StringBuilder(String.valueOf(sqla))).append(sqlb).toString();
							oa_db.executeUpdate(sql);
							for (Iterator ite = rsList.iterator(); ite.hasNext(); oa_db.executeUpdate(sql2))
							{
								elem = (String[])ite.next();
								sql2 = (new StringBuilder("insert into oa_workflow(config_id,object_ID,describe1,describe2,type_id) values ('")).append(elem[0]).append("','").append(publicize_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("','02')").toString();
							}

						}
						response.sendRedirect("oa/publicize/change_ok_b.jsp");
					}
				}
				catch (Exception ex)
				{
					response.sendRedirect((new StringBuilder("oa/publicize/change_ok_c.jsp?publicize_ID=")).append(publicize_ID).toString());
				}
				oa_db.commit();
				oa_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
